/*
 * $Id: SkvEnv.h,v 1.4 2006-04-24 08:42:10 bacon Exp $
 */

#ifndef _XPP_UTL_SKVENV_CLASS_
#define _XPP_UTL_SKVENV_CLASS_

#include <xpp/utl/SkvList.h>
#include <xpp/bas/LinkedList.h>

namespace xpp
{
	namespace utl
	{

		class SkvEnv: protected SkvList 
		{
		public:
			enum 
			{
				MAX_SCTN_LEN = 64,
				MAX_KEY_LEN  = 64,
 				// 64 for sctn, 64 for key, 1 for splitter
				MAX_NAME_LEN = 129,
				// the default value can't be longer than this
				MAX_DVAL_LEN = 256
			};
		
			typedef int (SkvEnv::*ProbeProc) (const xp_char_t* val);
			struct Item 
			{
				xp_char_t sctn[MAX_SCTN_LEN + 1];
				xp_char_t key [MAX_KEY_LEN  + 1];
				xp_char_t name[MAX_NAME_LEN + 1];
				xp_char_t dval[MAX_DVAL_LEN + 1]; // default value
				ProbeProc probe;
			};
			typedef xpp::bas::LinkedList<Item> ItemList;
		
			SkvEnv (xp_size_t mpb_size = 0): SkvList (mpb_size) {}

			int addItem (const xp_char_t* name,
				const xp_char_t* dval, ProbeProc probe);
			int removeItem (const xp_char_t* name);
		
			xp_char_t* itemName (const xp_char_t* scnt, const xp_char_t* key);
			xp_char_t* itemDval (const xp_char_t* scnt, const xp_char_t* key);
			int  getItemNameAndDval (
				const xp_char_t* scnt, const xp_char_t* key, 
				xp_char_t** name, xp_char_t** val);
		
			const ItemList& itemList () const 
			{
				return item_list;
			}
		
			const xp_char_t* itemValueOf (const xp_char_t* name) const;
			const xp_char_t* setItemValue (
				const xp_char_t* name, const xp_char_t* val);
		
			static int split_name (
				const xp_char_t* name, xp_char_t** sctn, 
				xp_size_t* sctnLen, xp_char_t** key, xp_size_t* keyLen);
			static xp_size_t make_name  (
				xp_char_t* buf, xp_size_t sz, 
				const xp_char_t* sctn, const xp_char_t* key);
		
			/*
			void print ();
			*/

		protected:
			ItemList  item_list;
		};
		
	}
}
#endif
		
